package com.yazi.mxz.admin.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.yazi.mxz.common.controller.BaseController;
import com.yazi.mxz.common.entity.beans.Admin;
import com.yazi.mxz.common.service.AdminService;
import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.tool.PageLink;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.jackson.result.StatusResult;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("adminController")
@RequestMapping("/admin")
public class AdminController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(AdminController.class);

	@Resource
	AdminService adminService;

	
	@RequestMapping("/login")
	public String login() {
		log.debug("进入登录页面");
		return "login";
	}
	
	@RequestMapping("/loginDo")
	public String loginDo(Admin admin, String captcha, Model model) {
		log.debug("登录");
		if (!checkCaptcha(captcha)) {
		}
		try {
			UsernamePasswordToken token = new UsernamePasswordToken();
			token.setUsername(admin.getName());
			token.setPassword(admin.getPassword().toCharArray());
			//token.setRememberMe(true);
			Subject subject = SecurityUtils.getSubject();
			subject.login(token);
		} catch (AuthenticationException e) {
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		
		return "redirect:/";
	}
	
	@RequestMapping("/logoutDo")
	public String logoutDo() {
		log.debug("退出登录");
		SecurityUtils.getSubject().logout();
		return "redirect:/admin";
	}
	
	@RequestMapping("/list")
	public String list(Admin admin, Searching search, Model model){
		log.debug("进入管理员列表页面");
		Page<Admin> page = adminService.list(admin, search);
		PageLink pageLink = new MyPageLink(page.getPaging());
		model.addAttribute("adminList", page.getList());
		model.addAttribute("pageLink", pageLink);
		model.addAttribute("searchWord", search.getSearchWord());
		return "admin_list";
	}
	
	@RequestMapping("/addOrEdit")
	public String addOrEdit(Long adminId, Model model){
		Admin admin = null;
		if (null != adminId) {
			log.debug("进入管理员账户添加页面:adminId={}", adminId);
			admin = adminService.one(adminId);
		} else {
			log.debug("进入管理员账户编辑页面");
		}
		model.addAttribute("admin", admin);
		return "admin_addOrEdit";
	}
	
	@RequestMapping("/addOrEditDo")
	public String addOrEditDo(Admin admin, Model model){
		log.debug("添加/编辑管理员账户信息:adminId={}", admin.getAdminId());
		try {
			adminService.addOrEdit(admin);
		} catch (MyException e) {
			log.debug("添加/编辑管理员账户信息失败:", e);
		} catch (Exception e) {
			log.error("添加/编辑管理员账户信息异常:", e);
		}
		return "redirect:/admin/list";
	}
	
	@RequestMapping("/role")
	public String role(Long adminId, Model model){
		log.debug("进入管理员角色设置页面");
		Admin admin = adminService.one(adminId);
		model.addAttribute("admin", admin);
		return "admin_role";
	}
	
	@RequestMapping("/roleDo/json")
	@ResponseBody
	public StatusResult roleDoJson(long adminId, long[] roleIds){
		log.debug("设置管理员角色:adminId={},roleIds={}", adminId, roleIds);
		StatusResult jsonResult = new StatusResult();
		try {
			adminService.editRoleForAdminId(adminId, roleIds);
			jsonResult.setOkMessage("提交成功");
		} catch (Exception e) {
			log.debug("设置管理员角色异常:", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		
		return jsonResult;
	}
	
	@RequestMapping(value="/noPerms",headers="X-Requested-With=XMLHttpRequest")
	@ResponseBody
	public StatusResult noPermsJson(HttpServletRequest request, Model model) {
		StatusResult jsonResult = new StatusResult();
		jsonResult.setFailMessage("您没有权限进行此操作!");
		return jsonResult;
	}
	
	@RequestMapping(value="/noPerms",headers="!X-Requested-With")
	public String noPerms(HttpServletRequest request, Model model) {
		return "admin_noPerms";	
	}

}
